const express = require("express");
const MemberLevel = require("../models/memberLevel");
const MemberInfo=require("../models/meberlnfo");
const Expenses=require('../models/expense');
const router = new express.Router();

router.post('/member',async(req,res)=>{
    const result =await MemberLevel.create(req.body);
    res.send("会员等级录入成功！")
});

router.get('/member_level_search',async(req,res)=>{
    const result = await MemberLevel.find({},{
        _id:0,
        __v:0
    });
    res.send(result);
})

router.post('/member_info',async(req,res)=>{
    const result=await MemberInfo.create(req.body);
    res.send("会员信息录入成功！！")
})

router.get("/member_search",async(req,res)=>{
    const result = await MemberInfo.find(req.query,{
        _id:0,
        __v:0
    });
    res.send(result);
})

router.post('/expense',async(req,res)=>{
    await Expenses.create(req.body);
    res.send("消费记录登记成功!");
})
router.get('/expense_search',async(req,res)=>{
    const result= await Expenses.aggregate([{
        $lookup:{
            from:'memberinfos',
            localField:'member_id',
            foreignField:'member_id',
            as:'expense_list'
        }
    },{
        $match:{
            member_id:req.query.member_id
        }
    }])
    res.send(result);
})

router.get('/')
module.exports=router